﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class frm_TuyenBay : Form
    {
        private TuyenBayBUS sbBUS = new TuyenBayBUS();
        private TuyenBayDTO sbDTO = new TuyenBayDTO();
        public frm_TuyenBay()
        {
            InitializeComponent();
        }
        private bool KiemTraNhap()
        {

            if (txtMaTB.Text == "" || txtSBDi.Text == "" || txtSBDen.Text == "")
                return false;
            else
                return true;
        }
        public void HienThiDSTuyenBay()
        {
            lvTuyenBay.FullRowSelect = true;
            lvTuyenBay.Items.Clear();

            int i;
            string str;
            DataTable dt;

            dt = sbBUS.LayDanhSach();
            for (i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem lvi;
                lvi = lvTuyenBay.Items.Add((i + 1).ToString());
                str = dt.Rows[i]["MATUYENBAY"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["SANBAYDI"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["SANBAYDEN"].ToString();
                lvi.SubItems.Add(str);
            }

        }



        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaTB.Text != "")
            {
                string str;
                bool ktlap = true;
                str = txtMaTB.Text;
                for (int i = 0; i < lvTuyenBay.Items.Count; i++)
                {
                    if (str.Trim().Equals(lvTuyenBay.Items[i].SubItems[1].Text.ToString().Trim()))
                    {
                        ktlap = false; break;
                    }
                    else
                        ktlap = true;
                }


                if (ktlap == true)
                {
                    sbDTO.Matuyenbay = txtMaTB.Text;
                    sbDTO.Sanbaydi = txtSBDi.Text;
                    sbDTO.Sanbayden = txtSBDen.Text;
                    sbBUS.Them(sbDTO);
                    HienThiDSTuyenBay();

                    MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SoanLai();
                }
                else
                    MessageBox.Show("Tuyen bay da co trong danh saCh", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                MessageBox.Show("Ban chua nhap ma tuyen bay", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void SoanLai()
        {

            txtMaTB.Text = "";
            txtSBDi.Text = "";
            txtSBDen.Text = "";
        }

        private void frm_TuyenBay_Load(object sender, EventArgs e)
        {
            HienThiDSTuyenBay();
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvTuyenBay_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvTuyenBay.SelectedIndices.Count > 0)
            {
                txtMaTB.Text = lvTuyenBay.SelectedIndices[0].ToString();
                txtSBDi.Text = lvTuyenBay.Items[lvTuyenBay.SelectedIndices[0]].SubItems[2].Text;
                txtSBDen.Text = lvTuyenBay.Items[lvTuyenBay.SelectedIndices[0]].SubItems[3].Text;
                txtMaTB.Text = lvTuyenBay.Items[lvTuyenBay.SelectedIndices[0]].SubItems[1].Text;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {


            if (lvTuyenBay.SelectedIndices.Count > 0)
            {
                string str = "";
                str = lvTuyenBay.Items[lvTuyenBay.SelectedIndices[0]].SubItems[1].Text;
                if (MessageBox.Show("Bạn có chắc chắn là muốn xóa san bay '" + str + "' không ?", "Hỏi lại", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    sbBUS.Xoa(lvTuyenBay.SelectedIndices[0]);
                    lvTuyenBay.Items.Remove(lvTuyenBay.SelectedItems[0]);
                    MessageBox.Show("Tuyen bay '" + txtMaTB.Text + "'da duoc xoa.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtMaTB.Text = "";
                    txtSBDi.Text = "";
                    txtSBDen.Text = "";
                }
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (lvTuyenBay.SelectedIndices.Count > 0)
            {

                sbDTO.Matuyenbay = txtMaTB.Text;
                sbDTO.Sanbaydi = txtSBDi.Text;
                sbDTO.Sanbayden = txtSBDen.Text;
                sbBUS.CapNhat(sbDTO);


                HienThiDSTuyenBay();
                MessageBox.Show("Tuyen Bay’" + txtMaTB.Text + "’ đã được cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SoanLai();

            }
            else
            {

                MessageBox.Show("Bạn phải chọn 1 san bay trong danh sách trước", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            txtMaTB.Text = "";
            txtSBDi.Text = "";
            txtSBDen.Text = "";
        }

        private void btnThem_Click_1(object sender, EventArgs e)
        {
            if (txtMaTB.Text != "")
            {
                string str;
                bool ktlap = true;
                str = txtMaTB.Text;
                for (int i = 0; i < lvTuyenBay.Items.Count; i++)
                {
                    if (str.Trim().Equals(lvTuyenBay.Items[i].SubItems[1].Text.ToString().Trim()))
                    {
                        ktlap = false; break;
                    }
                    else
                        ktlap = true;
                }


                if (ktlap == true)
                {
                    sbDTO.Matuyenbay = txtMaTB.Text;
                    sbDTO.Sanbaydi = txtSBDi.Text;
                    sbDTO.Sanbayden = txtSBDen.Text;
                    sbBUS.Them(sbDTO);
                    HienThiDSTuyenBay();

                    MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SoanLai();
                }
                else
                    MessageBox.Show("Tuyen bay da co trong danh saCh", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                MessageBox.Show("Ban chua nhap ma tuyen bay", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

       

      


    }
}
